package com.app.buss.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.buss.R;
import com.app.buss.bean.TopicBean;
import com.app.buss.ui.CommentActivity;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.MViewHolder> {
    private List<TopicBean> datas;
    private Context context;

    public TopicAdapter(List<TopicBean> datas, Context context){
     this.datas = datas;
     this.context = context;
    }

    @NonNull
    @Override
    public MViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_topic, parent, false);
        return new MViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MViewHolder holder, int position) {
        holder.name.setText(datas.get(position).getName());
        holder.des.setText(datas.get(position).getDes());
        holder.time.setText(datas.get(position).getTime());
        holder.likeNum.setText(datas.get(position).getLikes()+"");
        holder.chatNum.setText(datas.get(position).getChats()+"");
        holder.likes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    holder.likeNum.setText(datas.get(position).getLikes()+1+"");
                }else {
                    holder.likeNum.setText(datas.get(position).getLikes()+"");
                }
            }
        });
        holder.chat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, CommentActivity.class)
                        .putExtra("data",datas.get(position)));
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class MViewHolder extends RecyclerView.ViewHolder{
        TextView name;
        TextView time;
        TextView des;
        TextView likeNum;
        TextView chatNum;
        CheckBox likes;
        ImageView chat;

        MViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.tv_name);
            time = itemView.findViewById(R.id.tv_time);
            des = itemView.findViewById(R.id.tv_des);
            likeNum = itemView.findViewById(R.id.tv_like_num);
            chatNum = itemView.findViewById(R.id.tv_chat_num);
            likes = itemView.findViewById(R.id.cb_like);
            chat = itemView.findViewById(R.id.img_chat);

        }
    }
}
